package com.itcam.reflect_.msb03;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * Author:Cammy.Wu
 * Date:2024-05-17
 * Description:
 */
/**
 *
 */
public class Test01 {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        // 获取字节码信息：
        Class cls = Student.class;

        // 通过字节码信息可以获取构造器
        Constructor[] c1 = cls.getConstructors();
        for (Constructor constructor : c1) {
            System.out.println("constructor = " + constructor);
        }

        System.out.println("--------------------------------");

        // getDeclaredConstructors:获取运行时类的全部修饰符的构造器
        Constructor[] c2 = cls.getDeclaredConstructors();
        for (Constructor constructor : c2) {
            System.out.println("constructor = " + constructor);
        }

        System.out.println("--------------------------------");

        // 获取指定的构造器
        // 得到空构造器
        Constructor constructor1 = cls.getConstructor();
        System.out.println("constructor1 = " + constructor1);

        // 得到两个参数的构造器
        Constructor constructor2 = cls.getConstructor(double.class, double.class);
        System.out.println("constructor2 = " + constructor2);

        // 得到一个参数的有参构造器，并且是private修饰的
        Constructor constructor3 = cls.getDeclaredConstructor(int.class);
        System.out.println("constructor3 = " + constructor3);

        // 有了构造器以后我们就可以创建对象
        Object o1 = constructor1.newInstance();
        System.out.println("o1 = " + o1);

        Object o2 = constructor2.newInstance(180.5, 170.5);
        System.out.println("o2 = " + o2);

    }
}
